<template lang="html">
    <div class="page-nav-bar" v-if="show">
      <van-nav-bar v-if="show" class="main-header" :title="title" :left-arrow="!hiddenLeft" :fixed="true" :z-index="999" @click-left="onClick"/>
    </div>
</template>

<script>

export default {
  props: {
    title: String,
    goUrl: String,
    hiddenLeft: Boolean
  },
  data () {
    return {
      show: false,
      isMini: false
    }
  },
  created () {
    this.browserRedirect()
  },
  methods: {
    onClick () {
      if (this.goUrl) {
        this.$router.push(this.goUrl)
      } else {
        this.$router.go(-1)
      }
    },
    browserRedirect () {
      let sUserAgent = navigator.userAgent.toLowerCase() // 输出：sUserAgent mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko) version/9.0 mobile/13b143 safari/601.1
      // let sUserAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148/yggiOSApp/1.0.0'
      // let bIsIpad = sUserAgent.match(/ipad/i) || false
      // let bIsIphoneOs = sUserAgent.match(/iphone/i) || false
      // let bIsMidp = sUserAgent.match(/midp/i) || false
      // let bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) || false
      // let bIsUc = sUserAgent.match(/ucweb/i) || false
      // let bIsAndroid = sUserAgent.match(/android/i) || false
      // let bIsCE = sUserAgent.match(/windows ce/i) || false
      // let bIsWM = sUserAgent.match(/windows mobile/i) || false
      let isyggiOSApp = sUserAgent.match(/yggiosapp/i) || false
      let isyggAndroidApp = sUserAgent.match(/android-ygg/i) || false
      let isMini = false
      let that = this
      if (sUserAgent.match(/micromessenger/i)) {
        // ios的ua中无miniProgram，但都有MicroMessenger（表示是微信浏览器）
        if (sUserAgent.match(/miniprogram/i)) {
          that.show = false
          window.wx.miniProgram.getEnv((res) => {
            isMini = res.miniprogram
            if (isMini) {
              that.show = false
            }
          })
        } else {
          // alert('微信')
        }
      } else {
        if (isyggiOSApp || isyggAndroidApp || isMini) {
          this.show = false
        } else {
          this.show = true
        }
      }

      // if (bIsIpad[0] || bIsIphoneOs[0] || bIsMidp[0] || bIsUc7[0] || bIsUc[0] || bIsAndroid[0] || bIsCE[0] || bIsWM[0]) {
      //   console.log('浏览器')
      //   this.show = true
      // } else {
      //   console.log('app')
      //   this.show = false
      // }
    }
  }
}
</script>
<style lang="scss" scoped>
.main-header {
  z-index: 99;
}
</style>
